from GameCoreController import GameCoreController
from Location import Direction
import os

class GameConsoleView:
    """
        控制台视图
    """

    def __init__(self):
        self.__controller = GameCoreController()

    def start(self):
        self.__controller.generate_new_number()
        self.__controller.generate_new_number()
        self.print_map()

    def print_map(self):
        """
            打印界面
        :return:
        """
        print("---------------------")
        os.system("clear")
        for r in range(len(self.__controller.map)):
            for c in range(len(self.__controller.map[r])):
                print(self.__controller.map[r][c], end=" ")
            print()

    def update(self):
        while True:
            self.move_map()
            # 如果界面发生变化
            if self.__controller.is_change:
                self.__controller.generate_new_number()
                self.print_map()
                if self.__controller.is_game_over():
                    print('游戏结束')
                    break

    def move_map(self):
        """
            移动位置
        :return:
        """
        dir = input('请输入移动方向(wsad)')
        if dir == 'w':
            self.__controller.move(Direction.up)
        elif dir == 's':
            self.__controller.move(Direction.down)
        elif dir == 'a':
            self.__controller.move(Direction.left)
        elif dir == 'd':
            self.__controller.move(Direction.right)
